package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.admin.Role;
import com.cskaoyan.bean.admin.bo.PermissionsBo;
import com.cskaoyan.bean.admin.vo.PermissionsVo;
import com.cskaoyan.mapper.admin.PermissionMapper;
import com.cskaoyan.service.admin.PermissionService;
import com.cskaoyan.service.admin.PermissionWithSystemService;
import com.cskaoyan.service.admin.RoleService;
import com.cskaoyan.utils.ReflectionSetJavaBeanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 类<code>Doc</code>用于：TODO
 *
 * @author Acher
 * @version 1.0
 * @date 2021-10-18
 */
@RequestMapping("admin/role")
@RestController
public class RoleController {

    @Autowired
    RoleService roleService;
    @Autowired
    PermissionWithSystemService permissionWithSystemService;
    @Autowired
    PermissionService permissionService;

    @RequiresPermissions("admin:role:permission:get")
    @RequestMapping("permissions")
    // http://localhost:8083/admin/role/permissions?roleId=1
    public BaseRespVo permissions(@Validated PermissionsBo permissionsBo, BindingResult bindingResult) {
        PermissionsVo permissionsVo = permissionWithSystemService.queryPermissions(permissionsBo.getRoleId());
        return BaseRespVo.ok(permissionsVo);
    }

    @RequiresPermissions("admin:role:permission:update")
    @PostMapping("permissions")
    // http://localhost:8083/admin/role/permissions
    public BaseRespVo updatePermissions(@RequestBody @Validated PermissionsBo permissionsBo, BindingResult bindingResult) {
        // 根据roleId添加或删除permission表数据
        // 直接替换表内数据 👉 先删后增
        permissionService.replace(permissionsBo);
        return BaseRespVo.ok();
    }

    @RequiresPermissions("admin:role:read")
    @RequestMapping("options")
    // http://localhost:8083/admin/role/options
    public BaseRespVo options() {
        PageDataVo pageDataVo = roleService.options();
        return BaseRespVo.ok(pageDataVo);
    }

    @RequiresPermissions("admin:role:list")
    @RequestMapping("list")
    // http://localhost:8083/admin/role/list?page=1&limit=20&sort=add_time&order=desc
    public BaseRespVo list(String name, BaseParam param) {
        PageDataVo pageDataVo = roleService.query(name, param);
        return BaseRespVo.ok(pageDataVo);
    }

    @RequiresPermissions("admin:role:update")
    @PostMapping("update")
    // http://localhost:8083/admin/role/update
    public BaseRespVo update(@RequestBody @Validated Role role, BindingResult bindingResult) {
        ReflectionSetJavaBeanUtils.setJavaBean(role);
        if (!roleService.update(role))
            return BaseRespVo.error("修改失败");
        return BaseRespVo.ok(role);
    }

    @RequiresPermissions("admin:role:create")
    @PostMapping("create")
    // http://localhost:8083/admin/role/create
    public BaseRespVo create(@RequestBody @Validated Role role, BindingResult bindingResult) {
        ReflectionSetJavaBeanUtils.setJavaBean(role);
        if (!roleService.create(role))
            return BaseRespVo.error("添加失败");
        return BaseRespVo.ok(role);
    }

    @RequiresPermissions("admin:role:delete")
    @PostMapping("delete")
    // http://localhost:8083/admin/role/delete
    public BaseRespVo delete(@RequestBody @Validated Role role, BindingResult bindingResult) {
        if (!roleService.delete(role))
            return BaseRespVo.error("删除失败");
        return BaseRespVo.ok(role);
    }
}
